#pragma once

#include <fstream>
#include <sstream>
#include <string>
#include <vector>

// 表示一行数据
using Row = std::vector<std::string>;
// 表示整个结果集
using CvsResultSet = std::vector<Row>;

struct CvsResult {
    std::string column_names;
    std::vector<std::vector<std::string>> rows;
};

// 加载CSV文件为CvsResult格式
CvsResult loadCSVResult(const std::string &filename) {
    CvsResult result;
    std::ifstream file(filename);
    std::string line;
    const char DELIMITER = '\x1F'; // ASCII单元分隔符

    // 读取列名行
    if (std::getline(file, line)) {
        std::istringstream ss(line);
        std::string column;
        bool first = true;

        while (std::getline(ss, column, ',')) {
            if (!first) {
                result.column_names += DELIMITER;
            }
            result.column_names += column + ",";
            first = false;
        }
    }

    // 读取数据行
    while (std::getline(file, line)) {
        std::istringstream ss(line);
        std::string field;
        Row row;

        while (std::getline(ss, field, ',')) {
            row.push_back(field);
        }

        if (!row.empty()) {
            result.rows.push_back(row);
        }
    }

    return result;
}
